Merge pull request #496 from ianblenke/replace-dockerfile

Added ianblenke/docker-huginn as docker/ subdirectory

Andrew Cantino 10 years ago
parent
commit
ae15ede9e3
6 changed files with 319 additions and 0 deletions
  1. 30 0
      docker/Dockerfile
  2. 0 0
      docker/Dockerfile.rbenv
  3. 2 0
      docker/Makefile
  4. 137 0
      docker/README.md
  5. 111 0
      docker/scripts/init
  6. 39 0
      docker/scripts/setup

+ 30 - 0
docker/Dockerfile

@@ -0,0 +1,30 @@
1
+FROM ubuntu:14.04
2
+MAINTAINER Andrew Cantino
3
+
4
+ENV DEBIAN_FRONTEND noninteractive
5
+RUN apt-get update && \
6
+    apt-get install -y software-properties-common && \
7
+    add-apt-repository -y ppa:git-core/ppa && \
8
+    add-apt-repository -y ppa:brightbox/ruby-ng && \
9
+    apt-get update && \
10
+    apt-get install -y build-essential checkinstall postgresql-client \
11
+      git-core mysql-server redis-server python2.7 python-docutils \
12
+      libmysqlclient-dev libpq-dev zlib1g-dev libyaml-dev libssl-dev \
13
+      libgdbm-dev libreadline-dev libncurses5-dev libffi-dev \
14
+      libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev \
15
+      graphviz libgraphviz-dev \
16
+      ruby2.1 ruby2.1-dev supervisor && \
17
+    gem install --no-ri --no-rdoc bundler && \
18
+    rm -rf /var/lib/apt/lists/*
19
+
20
+ADD scripts/ /scripts
21
+RUN chmod 755 /scripts/setup /scripts/init
22
+
23
+RUN /scripts/setup
24
+
25
+VOLUME /var/lib/mysql
26
+
27
+EXPOSE 5000
28
+
29
+CMD ["/scripts/init"]
30
+

Dockerfile → docker/Dockerfile.rbenv


+ 2 - 0
docker/Makefile

@@ -0,0 +1,2 @@
1
+build:
2
+	docker build -t cantino/huginn .

+ 137 - 0
docker/README.md

@@ -0,0 +1,137 @@
1
+Huginn for docker with multiple container linkage
2
+=================================================
3
+
4
+This image runs a linkable [Huginn](https://github.com/cantino/huginn) instance.
5
+
6
+There is an automated build repository on docker hub for [cantino/huginn](https://registry.hub.docker.com/builds/github/cantino/huginn/).
7
+
8
+This was patterned after [sameersbn/gitlab](https://registry.hub.docker.com/u/sameersbn/gitlab) by [ianblenke/huginn](http://github.com/ianblenke/huginn), and imported here for official generation of a docker hub auto-build image.
9
+
10
+The scripts/init script generates a .env file containing the variables as passed as per normal Huginn documentation.
11
+The same environment variables that would be used for Heroku PaaS deployment are used by this script.
12
+
13
+The scripts/init script is aware of mysql and postgres linked containers through the environment variables:
14
+
15
+    MYSQL_PORT_3306_TCP_ADDR
16
+    MYSQL_PORT_3306_TCP_PORT
17
+
18
+and
19
+
20
+    POSTGRESQL_PORT_5432_TCP_ADDR
21
+    POSTGRESQL_PORT_5432_TCP_PORT
22
+
23
+Its recommended to use an image that allows you to create a database via environmental variables at docker run, like `paintedfox / postgresql` or `centurylink / mysql`, so the db is populated when this script runs.
24
+
25
+If you do not link a database container, a built-in mysql database will be started.
26
+There is an exported docker volume of /var/lib/mysql to allow persistence of that mysql database.
27
+
28
+Additionally, the database variables may be overridden from the above as per the standard Huginn documentation:
29
+
30
+    HUGINN_DATABASE_ADAPTER #(must be either 'postgres' or 'mysql2')
31
+    HUGINN_DATABASE_HOST
32
+    HUGINN_DATABASE_PORT
33
+
34
+This script will run database migrations (rake db:migrate) which should be idempotent.
35
+
36
+It will also seed the database (rake db:seed) unless this is defined:
37
+
38
+    DO_NOT_SEED
39
+
40
+This same seeding initially defines the "admin" user with a default password of "password" as per the standard Huginn documentation.
41
+
42
+If you do not wish to have the default 6 agents, you will want to set the above environment variable after your initially deploy, otherwise they will be added automatically the next time a container pointing at the database is spun up.
43
+
44
+The CMD launches Huginn via the scripts/init script. This may become the ENTRYPOINT later.  It does take under a minute for Huginn to come up.  Use environmental variables that match your DB's creds to ensure it works.
45
+
46
+## Usage
47
+
48
+Simple stand-alone usage:
49
+
50
+    docker run -it -p 5000:5000 cantino/huginn
51
+
52
+To link to another mysql container, for example:
53
+
54
+    docker run --rm --name newcentury_mysql -p 3306 \
55
+        -e HUGINN_MYSQL_DATABASE=huginn \
56
+        -e HUGINN_MYSQL_USER=huginn \
57
+        -e HUGINN_MYSQL_PASSWORD=somethingsecret \
58
+        -e HUGINN_MYSQL_ROOT_PASSWORD=somethingevenmoresecret \
59
+        cantino/huginn
60
+    docker run --rm --name huginn --link newcentury_mysql:MYSQL -p 5000:5000 \
61
+        -e HUGINN_DATABASE_NAME=huginn \
62
+        -e HUGINN_DATABASE_USER=huginn \
63
+        -e HUGINN_DATABASE_PASSWORD=somethingsecret \
64
+        cantino/huginn
65
+
66
+To link to another container named 'postgres':
67
+
68
+    docker run --rm --name huginn --link POSTGRES:mysql -p 5000:5000 -e "DATABASE_USER=huginn" -e "DATABASE_PASSWORD=pass@word" cantino/huginn
69
+
70
+## Environment Variables
71
+
72
+Other Huginn 12factored environment variables of note, as generated and put into the .env file as per Huginn documentation,
73
+with an additional `HUGINN_` prefix to the variable.
74
+
75
+These are:
76
+
77
+    HUGINN_APP_SECRET_TOKEN
78
+    HUGINN_DOMAIN
79
+    HUGINN_ASSET_HOST
80
+    HUGINN_DATABASE_ADAPTER
81
+    HUGINN_DATABASE_ENCODING
82
+    HUGINN_DATABASE_RECONNECT
83
+    HUGINN_DATABASE_NAME
84
+    HUGINN_DATABASE_POOL
85
+    HUGINN_DATABASE_USERNAME
86
+    HUGINN_DATABASE_PASSWORD
87
+    HUGINN_DATABASE_HOST
88
+    HUGINN_DATABASE_PORT
89
+    HUGINN_DATABASE_SOCKET
90
+    HUGINN_RAILS_ENV
91
+    HUGINN_FORCE_SSL
92
+    HUGINN_INVITATION_CODE
93
+    HUGINN_SMTP_DOMAIM
94
+    HUGINN_SMTP_USER_NAME
95
+    HUGINN_SMTP_PASSWORD
96
+    HUGINN_SMTP_SERVER
97
+    HUGINN_SMTP_PORT
98
+    HUGINN_SMTP_AUTHENTICATION
99
+    HUGINN_SMTP_ENABLE_STARTTLS_AUTO
100
+    HUGINN_EMAIL_FROM_ADDRESS
101
+    HUGINN_AGENT_LOG_LENGTH
102
+    HUGINN_TWITTER_OAUTH_KEY
103
+    HUGINN_TWITTER_OAUTH_SECRET
104
+    HUGINN_THIRTY_SEVEN_SIGNALS_OAUTH_KEY
105
+    HUGINN_THIRTY_SEVEN_SIGNALS_OAUTH_SECRET
106
+    HUGINN_GITHUB_OAUTH_KEY
107
+    HUGINN_GITHUB_OAUTH_SECRET
108
+    HUGINN_AWS_ACCESS_KEY_ID
109
+    HUGINN_AWS_ACCESS_KEY
110
+    HUGINN_AWS_SANDBOX
111
+    HUGINN_FARADAY_HTTP_BACKEND
112
+    HUGINN_DEFAULT_HTTP_USER_AGENT
113
+    HUGINN_ALLOW_JSONPATH_EVAL
114
+    HUGINN_ENABLE_INSECURE_AGENTS
115
+    HUGGIN_ENABLE_SECOND_PRECISION_SCHEDULE
116
+    HUGINN_USE_GRAPHVIZ_DOT
117
+    HUGINN_TIMEZONE
118
+    HUGGIN_FAILED_JOBS_TO_KEEP
119
+
120
+
121
+The above environment variables will override the defaults. The defaults are read from the [.env.example](https://github.com/cantino/huginn/blob/master/.env.example) file.
122
+
123
+For variables in the .env.example that are commented out, the default is to not include that variable in the generated .env file.
124
+
125
+## Building on your own
126
+
127
+You don't need to do this on your own, because there is an [automated build](https://registry.hub.docker.com/u/cantino/huginn/) for this repository, but if you really want:
128
+
129
+    docker build --rm=true --tag={yourname}/huginn .
130
+
131
+## Source
132
+
133
+The source is [available on GitHub](https://github.com/cantino/huginn/).
134
+
135
+Please feel free to submit pull requests and/or fork at your leisure.
136
+
137
+

+ 111 - 0
docker/scripts/init

@@ -0,0 +1,111 @@
1
+#!/bin/bash
2
+set -e
3
+
4
+cd /app
5
+
6
+# Default to the environment variable values set in .env.example
7
+source /app/.env.example
8
+
9
+# is a mysql or postgresql database linked?
10
+# requires that the mysql or postgresql containers have exposed
11
+# port 3306 and 5432 respectively.
12
+if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ]; then
13
+  HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-mysql2}
14
+  HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
15
+  HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${MYSQL_PORT_3306_TCP_PORT}}
16
+elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
17
+  HUGINN_DATABASE_ADAPTER=${HUGINN_DATABASE_ADAPTER:-postgres}
18
+  HUGINN_DATABASE_HOST=${HUGINN_DATABASE_HOST:-${POSTGRESQL_PORT_5432_TCP_ADDR}}
19
+  HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
20
+fi
21
+
22
+grep = ../.env.example | sed -e 's/^#//' | grep -v -e '^#' | cut -d= -f1 | \
23
+  while read var ; do
24
+    echo "$var=\${HUGINN_$var:-\$$var}"
25
+  done > /app/.env
26
+
27
+chmod ugo+r /app/.env
28
+source /app/.env
29
+
30
+DATABASE_HOST=${HUGINN_DATABASE_HOST:-${DATABASE_HOST:-localhost}}
31
+DATABASE_ENCODING=${HUGINN_DATABASE_ENCODING:-${DATABASE_ENCODING}}
32
+USE_GRAPHVIZ_DOT=${HUGINN_USE_GRAPHVIZ_DOT:-${USE_GRAPHVIZ_DOT}}
33
+
34
+# use default port number if it is still not set
35
+case "${DATABASE_ADAPTER}" in
36
+  mysql2) DATABASE_PORT=${DATABASE_PORT:-3306} ;;
37
+  postgres) DATABASE_PORT=${DATABASE_PORT:-5432} ;;
38
+  *) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
39
+esac
40
+
41
+# start supervisord
42
+/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
43
+
44
+# start mysql server if ${DATABASE_HOST} is localhost
45
+if [ "${DATABASE_HOST}" == "localhost" ]; then
46
+  if [ "${DATABASE_ADAPTER}" == "postgres" ]; then
47
+    echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
48
+    exit 1
49
+  fi
50
+
51
+  # configure supervisord to start mysql (manual)
52
+  cat > /etc/supervisor/conf.d/mysqld.conf <<EOF
53
+[program:mysqld]
54
+priority=20
55
+directory=/tmp
56
+command=/usr/bin/mysqld_safe
57
+user=root
58
+autostart=false
59
+autorestart=true
60
+stdout_logfile=/var/log/supervisor/%(program_name)s.log
61
+stderr_logfile=/var/log/supervisor/%(program_name)s.log
62
+EOF
63
+  supervisorctl reload
64
+
65
+  # fix permissions and ownership of /var/lib/mysql
66
+  chown -R mysql:mysql /var/lib/mysql
67
+  chmod 700 /var/lib/mysql
68
+
69
+  # initialize MySQL data directory
70
+  if [ ! -d /var/lib/mysql/mysql ]; then
71
+    mysql_install_db --user=mysql
72
+  fi
73
+
74
+  echo "Starting mysql server..."
75
+  supervisorctl start mysqld >/dev/null
76
+
77
+  # wait for mysql server to start (max 120 seconds)
78
+  timeout=120
79
+  while ! mysqladmin -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} status >/dev/null 2>&1
80
+  do
81
+    timeout=$(expr $timeout - 1)
82
+    if [ $timeout -eq 0 ]; then
83
+      echo "Failed to start mysql server"
84
+      exit 1
85
+    fi
86
+    sleep 1
87
+  done
88
+
89
+  if ! echo "USE ${DATABASE_NAME}" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} >/dev/null 2>&1; then
90
+    DB_INIT="yes"
91
+    echo "CREATE DATABASE IF NOT EXISTS \`${DATABASE_NAME}\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
92
+    echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \`${DATABASE_NAME}\`.* TO 'root'@'localhost';" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
93
+  fi
94
+fi
95
+
96
+# Assuming we have a created database, run the migrations and seed it idempotently.
97
+[ -z "${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
98
+[ -z "${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
99
+
100
+[ -n "$INTENTIONALLY_SLEEP" ] && sleep $INTENTIONALLY_SLEEP
101
+
102
+# Fixup the Procfile and prepare the PORT
103
+[ -z "${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
104
+perl -pi -e 's/rails server$/rails server -p \$PORT/' /app/Procfile
105
+export PORT
106
+
107
+# Start huginn
108
+sudo -u huginn -EH bundle exec foreman start
109
+
110
+# As the ENTRYPOINT script, when this exits the docker container will Exit.
111
+exit 0

+ 39 - 0
docker/scripts/setup

@@ -0,0 +1,39 @@
1
+#!/bin/bash
2
+set -e
3
+
4
+# Initialize variables used by Huginn at installation time
5
+
6
+# Huginn is 12factor aware, embrace that fact for use inside of docker
7
+ON_HEROKU=${ON_HEROKU:-true}
8
+
9
+# Shallow clone the huginn project repo
10
+git clone --depth 1 https://github.com/cantino/huginn /app
11
+
12
+cd /app
13
+
14
+# add a huginn group and user
15
+adduser --group huginn
16
+adduser --disabled-login --ingroup huginn --gecos 'Huginn' --no-create-home --home /app huginn
17
+adduser huginn sudo
18
+passwd -d huginn
19
+
20
+# Change the ownership to huginn
21
+chown -R huginn:huginn /app
22
+
23
+# create required tmp and log directories
24
+sudo -u huginn -H mkdir -p tmp/pids tmp/cache tmp/sockets log
25
+chmod -R u+rwX log tmp
26
+
27
+# install gems required by Huginn, use local cache if available
28
+if [ -d "/scripts/cache" ]; then
29
+  mv /scripts/cache vendor/
30
+  chown -R huginn:huginn vendor/cache
31
+fi
32
+sudo -u huginn -H bundle install --deployment --without development test
33
+
34
+# silence setlocale message (THANKS DEBIAN!)
35
+cat > /etc/default/locale <<EOF
36
+LC_ALL=en_US.UTF-8
37
+LANG=en_US.UTF-8
38
+EOF
39
+